﻿using System;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using TheGameClient.Webservices;

namespace TheGameClient
{
	public enum SystemFileType
	{
		Data = 0x00,
		DataWithContent = 0x01,
		Program = 0x02
	}

	public partial class DlgCreateNewFile
	{
		public DlgCreateNewFile()
		{
			InitializeComponent();
			Type fileType = typeof (SystemFileType);
			var fields = (from field in fileType.GetFields(BindingFlags.Static | BindingFlags.Public) select field.Name);
			cmbFileType.ItemsSource = fields;

			NewFile = new SystemFile();
			NewFile.Id = Guid.NewGuid().ToLong();
		}

		private SystemFile NewFile { get; set; }

		public SystemDirectory ParentDirectory { private get; set; }

		private void OkButtonClick(object sender, RoutedEventArgs e)
		{
			return;
			//NewFile.fileId = Guid.NewGuid();
			//DateTime filetime;
			//SystemFileType ftype;
			//try
			//{
			//    filetime = DateTime.Parse(txtFileDate.Text);
			//    ftype = (SystemFileType)Enum.Parse(typeof(SystemFileType), cmbFileType.SelectedItem.ToString(), false);
			//}
			//catch (Exception)
			//{
			//    return;
			//}
			//U2SvcClient client = App.Client;

			//if (ftype == SystemFileType.Program)
			//{
			//    ComputerProgram prog = cmbPrograms.SelectedItem as ComputerProgram;
			//    if(prog != null)
			//    {
			//        if (!prog.canModifyCopy)
			//        {
			//            client.CreateProgramCopyCompleted += (_s, _e) =>
			//            {
			//                if (_e.Result)
			//                {
			//                    DialogResult = true;
			//                    return;
			//                }
			//            };
			//            client.CreateProgramCopyAsync(App.InternalSessionId, prog.programId, ParentDirectory.systemDirectoryId);
			//        }
			//        else
			//        {
			//            NewFile.fileTime = filetime;
			//            NewFile.FileType = ftype;
			//            NewFile.parentDirectory = ParentDirectory;
			//            NewFile.encryptionLevel = (byte)numEncryptionLevel.Value;
			//            NewFile.compressionLevel = (byte)numCompressionLevel.Value;
			//            NewFile.fileName = txtFileName.Text;
			//            NewFile.fileSize = (int)numFileSize.Value;
			//            NewFile.ComputerProgram = prog;
			//            NewFile.parentDirectory = ParentDirectory;
			//            client.CreateModifiedProgramCopyCompleted += (_s, _e) =>
			//            {
			//                if (_e.Result)
			//                {
			//                    DialogResult = true;
			//                    return;
			//                }
			//            };
			//            client.CreateModifiedProgramCopyAsync(App.InternalSessionId, NewFile);
			//        }
			//    }
			//}
			//else
			//{
			//    NewFile.fileTime = filetime;
			//    NewFile.FileType = ftype;
			//    NewFile.parentDirectory = ParentDirectory;
			//    NewFile.encryptionLevel = (byte)numEncryptionLevel.Value;
			//    NewFile.compressionLevel = (byte)numCompressionLevel.Value;
			//    NewFile.fileName = txtFileName.Text;
			//    NewFile.fileSize = (int)numFileSize.Value;
			//    if (ftype == SystemFileType.DataWithContent)
			//        NewFile.fileContent = txtFileContent.Text;

			//    client.CreateSystemFileCompleted += (_s, _e) =>
			//    {
			//        if (_e.Result == true)
			//            this.DialogResult = true;
			//    };
			//    client.CreateSystemFileAsync(App.InternalSessionId, NewFile);
			//}
		}

		private void CancelButtonClick(object sender, RoutedEventArgs e)
		{
			DialogResult = false;
		}

		private void CmbFileTypeSelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			SystemFileType type =
				(SystemFileType) Enum.Parse(typeof (SystemFileType), cmbFileType.SelectedItem.ToString(), false);
			switch (type)
			{
			case SystemFileType.Data:
				stkFileName.Visibility = Visibility.Visible;
				stkFileSize.Visibility = Visibility.Visible;
				stkFileTime.Visibility = Visibility.Visible;
				stkFileContent.Visibility = Visibility.Visible;
				stkCompression.Visibility = Visibility.Visible;
				stkEncryption.Visibility = Visibility.Visible;
				stkProgramFileContent.Visibility = Visibility.Collapsed;
				stkTextFileContent.Visibility = Visibility.Collapsed;
				break;
			case SystemFileType.DataWithContent:
				stkFileName.Visibility = Visibility.Visible;
				stkFileSize.Visibility = Visibility.Visible;
				stkFileTime.Visibility = Visibility.Visible;
				stkFileContent.Visibility = Visibility.Visible;
				stkCompression.Visibility = Visibility.Visible;
				stkEncryption.Visibility = Visibility.Visible;
				stkProgramFileContent.Visibility = Visibility.Collapsed;
				stkTextFileContent.Visibility = Visibility.Visible;
				break;
			case SystemFileType.Program:
				stkFileName.Visibility = Visibility.Collapsed;
				stkFileSize.Visibility = Visibility.Collapsed;
				stkFileTime.Visibility = Visibility.Collapsed;
				stkCompression.Visibility = Visibility.Collapsed;
				stkEncryption.Visibility = Visibility.Collapsed;
				stkProgramFileContent.Visibility = Visibility.Visible;
				stkTextFileContent.Visibility = Visibility.Collapsed;
				cmbPrograms.Visibility = Visibility.Visible;
				break;
			}
		}

		private void CmbProgramsSelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			ProgramFile prog = cmbPrograms.SelectedItem as ProgramFile;
			if (prog == null)
				return;
			if (prog.AllowModify)
			{
				stkFileName.Visibility = Visibility.Visible;
				stkFileSize.Visibility = Visibility.Visible;
				stkFileTime.Visibility = Visibility.Visible;
				stkCompression.Visibility = Visibility.Visible;
				stkEncryption.Visibility = Visibility.Visible;
				stkProgramVersion.Visibility = Visibility.Visible;
				txtFileName.Text = prog.Program.Name; //todo assign program's name.
				numFileSize.Value = prog.DisplaySize;
				numProgramVersion.Value = prog.Version;
			}
			else
			{
				stkFileName.Visibility = Visibility.Collapsed;
				stkFileSize.Visibility = Visibility.Collapsed;
				stkFileTime.Visibility = Visibility.Collapsed;
				stkCompression.Visibility = Visibility.Collapsed;
				stkEncryption.Visibility = Visibility.Collapsed;
				stkProgramVersion.Visibility = Visibility.Collapsed;
			}
		}
	}
}